//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    public class SQLConnectionParameters : ConnectionParametersBase
    {
        public static IConnMgrInfoParams CreateTrustedConnection(string name, string server, string database)
        {
            SQLConnectionParameters ret = new SQLConnectionParameters(Guid.NewGuid(), name, server, true, null, null, database, false);
            return ret;
        }

        public static IConnMgrInfoParams CreateUserPassConnection(string name, string user, string pass, string server, string database)
        {
            SQLConnectionParameters ret = new SQLConnectionParameters(Guid.NewGuid(), name, server, false, user, pass, database, false);
            return ret;
        }

        private SQLConnectionParameters(Guid id, string name, string server, bool trusted, string user, string pass, string database, bool encrypted)
            : base(id, name)
        {
            Update(name, server, trusted, user, pass, database, encrypted);
        }

        internal void Load(out string serverName, out bool trustedConn, out bool userPass, out string username, out string password,
            out bool encrypted, out string database)
        {
            serverName = (string)Parameters["servername"];
            trustedConn = (bool)Parameters["trusted"];
            userPass = !trustedConn;
            username = (string)Parameters["username"];
            password = (string)Parameters["password"];
            database = (string)Parameters["database"];
            encrypted = (bool)Parameters["passwordEncrypted"];
        }

        internal void Update(string name, string server, bool trusted, string user, string pass, string database, bool encrypted)
        {
            Name = name;
            Parameters["servername"] = server;
            Parameters["trusted"] = trusted;
            Parameters["username"] = user;
            Parameters["password"] = pass;
            Parameters["database"] = database;
            Parameters["passwordEncrypted"] = encrypted;
        }

        private static SQLConnectionParameters FromXml(datasource.DataSource dataSource)
        {
            SQLConnectionParameters ret = new SQLConnectionParameters(
                new Guid(dataSource.id),
                dataSource.name,
                dataSource.GetPropertyAsString("servername"),
                dataSource.GetPropertyAsBoolean("trusted"),
                dataSource.GetPropertyAsString("username"),
                dataSource.GetPropertyAsString("password"),
                dataSource.GetPropertyAsString("database"),
                dataSource.GetPropertyAsBoolean("passwordEncrypted"));
            return ret;
        }

        private datasource.DataSource ToXml()
        {
            datasource.DataSource ret = new datasource.DataSource();
            ret.id = this.ID.ToString();
            ret.name = this.Name;
            ret.SetPropertyString("servername", (string)Parameters["servername"]);
            ret.SetPropertyBoolean("trusted", (bool)Parameters["trusted"]);
            ret.SetPropertyString("username", (string)Parameters["username"]);
            ret.SetPropertyString("password", (string)Parameters["password"]);
            ret.SetPropertyString("database", (string)Parameters["database"]);
            ret.SetPropertyBoolean("passwordEncrypted", (bool)Parameters["passwordEncrypted"]);
            return ret;
        }

        #region Connection Parameters Helpers
        public class Helper : IConnMgrInfoParamsHelper
        {
            public IConnMgrInfoParams FromXml(datasource.DataSource dataSource)
            {
                return SQLConnectionParameters.FromXml(dataSource);
            }

            public datasource.DataSource ToXml(IConnMgrInfoParams preset)
            {
                return ((SQLConnectionParameters)preset).ToXml();
            }

            public bool HandlesConnection(IConnMgrInfoParams conn)
            {
                return (conn is SQLConnectionParameters);
            }

            public Connection OpenConnection(IConnMgrInfoParams preset)
            {
                SQLConnectionParameters connInfo = (SQLConnectionParameters)preset;
                Connection conn = SQLConnection.Create(connInfo);
                conn.Open();
                return conn;
            }

            public bool TestConnection(IConnMgrInfoParams preset, out BackendStorageException error)
            {
                SQLConnectionParameters connInfo = (SQLConnectionParameters)preset;
                Connection conn = SQLConnection.Create(connInfo);
                return conn.Test(out error);
            }
        }
        #endregion
    }
}
